package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.ResultSearchBean.ResultSearchListBean.SearchResultCategory;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.util.PreferenceUtils;

import java.util.ArrayList;

public class FilterGridTitleAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context context;
    private boolean isChoose[];
    private ArrayList<SearchResultCategory> categorylist;

    public FilterGridTitleAdapter(Context context,
                                  ArrayList<SearchResultCategory> categorylist) {
        this.context = context;
        this.categorylist = MyApplication.getInstance().getCategorylist();
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return categorylist != null ? categorylist.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder6 holder = new Holder6();

        try {
            convertView = inflater.inflate(R.layout.ouka_filter_item_gv_item, null);

            SearchResultCategory resultCategory = categorylist.get(position);

            holder.itemTextView = (TextView) convertView
                    .findViewById(R.id.ouka_filter_gv_item_tv);
            holder.itemTextView.setText(resultCategory.getName());
            if (resultCategory.isSelect()) {
                holder.itemTextView
                        .setBackgroundResource(R.drawable.ouka_filter_select_check);
                holder.itemTextView.setTextColor(Color.parseColor("#FF0000"));
            } else {
                holder.itemTextView
                        .setBackgroundResource(R.drawable.ouka_filter_select_normal);
                holder.itemTextView.setTextColor(Color.parseColor("#333333"));
            }

            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return convertView;
    }

    public void chooceState(int position) {

        try {
            String cid = categorylist.get(position).getId();

            if (MyApplication.getInstance().getCategorylist().get(position)
                    .isSelect() == true) {
                MyApplication.getInstance().getCategorylist().get(position)
                        .setSelect(false);

                PreferenceUtils.getInstance().SetSettingString(PreferencesConfig.CID, "");
            } else {
                for (int i = 0; i < MyApplication.getInstance().getCategorylist()
                        .size(); i++) {
                    MyApplication.getInstance().getCategorylist().get(i)
                            .setSelect(false);
                }
                MyApplication.getInstance().getCategorylist().get(position)
                        .setSelect(true);

                PreferenceUtils.getInstance().SetSettingString(PreferencesConfig.CID, cid);
            }
            this.notifyDataSetChanged();

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

}

class Holder6 {

    TextView itemTextView;
}
